<?php

namespace App\Jobs\Cruise;

use App\Console\Commands\Cruise\CruiseCheckSelfTaskGenerateScheduleCommand;
use App\Models\Cruise\CruiseCheckSelfTaskModel;
use App\Models\Cruise\CruiseTemplateModel;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CruiseCheckSelfTaskInsertOrUpdateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public static string $QUEUE = "cruise:checkSelfTaskInsertOrUpdateJob";

    const INSERT = 0;
    const UPDATE = 1;

    public int $id;
    public int $channel;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(int $channel, int $id)
    {
        $this->channel = $channel;
        $this->id      = $id;
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws Exception
     */
    public function handle()
    {
        try{
            $cruiseCheckSelfTaskModel = CruiseCheckSelfTaskModel::query()->where("id", $this->id)
                                                                ->where("taskStartTime", "<=", time())
                                                                ->where("taskEndTime", ">=", time())
                                                                ->where("status", CruiseCheckSelfTaskModel::STATUS_ON)
                                                                ->first();


            if ($cruiseCheckSelfTaskModel) {
                switch ($cruiseCheckSelfTaskModel->templateType) {
                    case CruiseTemplateModel::TYPE_8:
                        CruiseCheckSelfTaskGenerateScheduleCommand::generateTaskDetail_8($cruiseCheckSelfTaskModel);
                        break;
                }
            }
        }catch (Exception $exception){
            logException("门店自检任务报错", $exception, $cruiseCheckSelfTaskModel->toArray());
        }
    }


}
